<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head th:replace="_fragments :: adminHeader(~{::title})">
    <link rel="stylesheet" type="text/css" href="../../static/semantic.min.css" th:href="@{/semantic.min.css}">
    <script
            src="https://code.jquery.com/jquery-3.1.1.min.js"
            integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
            crossorigin="anonymous"></script>
    <script src="../static/semantic.min.js" th:href="@{/semantic.min.js}"></script>
    <meta charset="UTF-8">
    <title>管理后台</title>
</head>
<body>
<div class="ui borderless inverted main menu" th:replace="_fragments :: adminMenu(1,blue)">
    <div class="ui huge inverted text container">
        <a class="item active orange" href="index.html" th:href="@{/admin/index}">
            <i class="home icon" ></i> 主页
        </a>
        <a class="item" href="category.html" th:href="@{/admin/category}">
            <i class="folder icon"></i> 分类
        </a>
        <a class="item" href="tags.html" th:href="@{/admin/tags}">
            <i class="tags icon"></i> 标签
        </a>
    </div>
</div>
<div class="ui borderless grid">
    <div class="ui two wide column"></div>
    <div class="ui eleven wide column">
        <div class="ui container">
            <form action="#" th:action="@{/admin/blog/search}" th:object="${searchBody}" method="post" class="ui segment form">
                <div class="field">
                    <div class="inline field">
                        <div class="field">
                            <input type="text" class="text" th:field="*{title}" th:value="*{title}" placeholder="标题">
                            <div class="ui selection dropdown">
                                <input type="hidden" th:field="*{categoryId}" th:value="*{categoryId}">
                                <div class="default text">分类</div>
                                <div class="menu">
                                    <div class="item" th:each="category: ${categoryList}" th:data-value="${category.id}" th:text="${category.name}">Java编程语言</div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="field">
                        <button id="searchBTN" class="ui teal basic mini button"><i class="search icon"></i>搜索</button>
                        <a th:href="@{/admin/blog/edit}" class="ui teal right floated button">新增</a>
                    </div>
                    <table class="ui table" >
                        <thead>
                        <tr>
                            <th>序号</th>
                            <th>标题</th>
                            <th>分类</th>
                            <th>更新时间</th>
                            <th>是否发布</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody id="blogList">
                        <tr th:each="blog,iterStat : ${pageInfo.getList()}" th:fragment="blogList" >
                            <td th:text="${blog.id}">1</td>
                            <td><a th:text="${blog.title}" th:href="@{/blog/} + ${blog.id}">Java编程的常用套路</a></td>
                            <td><span th:each="category:${categories.getCategoriesByArticleID(blog.id)}" ><a href="#" th:href="@{'/category/' + ${category.id}}" th:text="' | '+${category.name} +' | '">持续集成</a> </span></td>
                            <td><time th:text="${#dates.format(blog.create_time,'yyyy-MM-dd hh:mm:ss')}">2020/04/01 13:30</time></td>
                            <td th:text="${blog.published ? '是':'否'}"></td>
                            <td><a href="#" th:href="@{/admin/blog/edit?id={id}(id=${blog.id})}" class="ui right mini teal basic button">编辑</a>
                                <a href="#" th:value="${blog.id}" class="ui right mini delete button">删除</a>
                            </td>
                        </tr>

                        </tbody>
                        <tfoot>
                        <th colspan="8" th:if="${pageInfo.getSize()} > 0 and ${#request.getRequestURI() != '/admin/blog/search'}">
                            <div class="ui mini pagination menu" >
                                <a class="item" id="pre" th:href="@{/admin/blog(page=${pageInfo.getPrePage()})}" th:unless="${pageInfo.isIsFirstPage()}">上一页</a>
                                <a class="item" id="next" th:href="@{/admin/blog(page=${pageInfo.getNextPage()})}" th:unless="${pageInfo.isIsLastPage()}">下一页</a>
                            </div>
                        </th>

                        </tfoot>

                    </table>

                </div>
            </form>
        </div>
    </div>
    <div class="ui three wide column"></div>
</div>
<script>
    $('.ui.dropdown').dropdown({
        on: 'hover'
    });
    //搜索刷新
    $('#searchBTN').click(function () {
        var url = '/admin/blog/search';
        $.ajax({
            url: url,
            type: 'POST',
            success: function (data) {
                $("#blogList").load(data);
            }
        })
    });
    $(".delete.button").click(function () {
        var bool = confirm("确定删除?");
        if (bool) {
            var id = $(this).attr("value");//获得参数的值
            var url = "/admin/blog/del/" + id;
            $.get(url, function (data, status) {
                window.location.reload();
                alert("删除成功");
            });
        } else {
            return;
        }

    });

</script>
</body>
</html>